<?php
namespace App\Edu\Services;

use App\Common\Facades\OfficeFacade;
use App\Edu\Models\EduPaperCateModel;
use App\Edu\Models\EduPaperModel;
use App\Edu\Models\EduPaperQuestionModel;
use App\Edu\Models\EduQuestionModel;
use App\Services\CommonBaseService;

class EduPaperService extends CommonBaseService
{
    protected $eduPaperCateModel;
    protected $eduPaperQuestionModel;
    protected $eduQuestionModel;

    public function __construct(EduPaperModel $model, 
        EduPaperCateModel $eduPaperCateModel, 
        EduPaperQuestionModel $eduPaperQuestionModel,
        EduQuestionModel $eduQuestionModel)
    {
        parent::__construct($model);
        $this->eduPaperCateModel = $eduPaperCateModel;
        $this->eduPaperQuestionModel = $eduPaperQuestionModel;
        $this->eduQuestionModel = $eduQuestionModel;
    }

    /** 
     * 保存试卷
     */
    public function saveInfo($data)
    {
        $paperSaveData = [
            'paper_type' => $data['paper_type'] ?? 1,
            'question_number' => 0,
            'price' => $data['price'] ?? 0,
            'original_price' => $data['original_price'] ?? 0,
        ];
        if (empty($data['paper_id'])) {
            $paperId = $this->model->insertData($paperSaveData);
        } else {
            $this->model->updateData($paperSaveData, ['paper_id' => $data['paper_id']]);
            $paperId = $data['paper_id'];
        }
        $question = $data['question'];
        $questionSaveData = [];
        $paperQuestionSaveData = [];
        foreach ($question as $key => $val) {
            $questionSaveData = [
                'question_title' => $val['question_title'], // 题目
                'question_type' => $val['question_type'],
                'question_option' => $val['question_option'], // 选项
                'question_answer' => $val['question_answer'], // 答案
                'question_exp' => $val['question_exp'], // 解析
                'is_paper' => 1,
            ];
            if (empty($val['question_id'])) {
                $questionId = $this->eduQuestionModel->insertData($questionSaveData);
            } else {
                $this->eduQuestionModel->updateData($questionSaveData, ['question_id' => $val['question_id']]);
                $questionId = $val['question_id'];
            }
            $paperQuestionSaveData[] = [
                'paper_id' => $paperId,
                'question_id' => $questionId,
                'question_type' => $val['question_type'],
                'question_sort' => $key
            ];
        }
        $this->eduPaperQuestionModel->deleteBy(['paper_id' => $paperId]);
        $this->eduPaperQuestionModel->insertAll($paperQuestionSaveData);
        return $paperId;
    }

    /** 
     * 删除试卷
     */
    public function deletePaper($paperId)
    {
        return $this->deleteBy(['paper_id' => $paperId]);
    }

    /** 
     * 启用，禁用
     */
    public function changePaperStatus($paperId)
    {
        $info = $this->findOneBy(['paper_id' => $paperId], 'status');
        return $this->updateBy(['paper_id' => $paperId], ['status' => $info['status'] == 1 ? 0 : 1]);
    }

    /** 
     * 列表
     */
    public function paperList($params)
    {
        $list = $this->model->getList($params, $this->getPageSize($params));
        return $list;
    }

    /** 
     * 详情
     */
    public function paperInfo($id, $userId = 0)
    {
        if (!empty($userId)) {
            // 判断是否购买过该试卷
        }
        $info = $this->model->findOneBy(['paper_id' => $id]);
        $info['question'] = $this->eduPaperQuestionModel->findBy([
            'paper_id' => $info['paper_id'],
            'status' => 0
        ]);
        return $info;
    }

    /** 
     * 获取试题分类
     */
    public function getQuestionType()
    {
        return $this->eduQuestionModel::QUESTION_TYPE;
    }

    /** 
     * 导入word试卷
     */
    public function importPaper($file, $jsonData)
    {
        OfficeFacade::getPDFInfo($file, $jsonData);
    }
}
